package net.zjitc.dao.impl;

import net.zjitc.dao.CategoryDao;
import net.zjitc.domain.Category;
import net.zjitc.domain.User;
import net.zjitc.utils.C3P0Utils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * 2 * @Author: 洪瑶
 * 3 * @Date: 2018/10/29 18:33
 * 4
 */
public class CategoryIDaompl implements CategoryDao {
    @Override
    public List<Category> sqlfindAll() throws SQLException {
        QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource());
        String sql = "select * from category";
        List<Category> list = (List<Category>) queryRunner.query(sql,new BeanListHandler(Category.class));
        return list;
    }

    @Override
    public Category sqlfindBy(int id) throws SQLException {
        QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource());
        String sql = "select * from category where cid = ?";
        Category category = queryRunner.query(sql, new BeanHandler<Category>(Category.class),id);
        return category;
    }

    @Override
    public boolean sqlupdate(Category category, String id) throws SQLException {
        QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource());
        String sql = "update category set cname = ? where cid = ?";
        int num = queryRunner.update(sql,new Object[]{category.getCname(),id});
        if (num > 0 )
        {
            return true;
        }
        return false;
    }

    @Override
    public void sqldelete(int id) throws SQLException {
        QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource());
        String sql = "delete from category where cid = ?";
        queryRunner.update(sql,id);
    }
}
